package com.seed.util;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryTable;
import com.mybatisflex.core.row.Row;
import com.seed.core.pojo.seed.HasOnes;

import java.util.Map;

public class HasOneKit {

    public static QueryCondition getQueryCondition(HasOnes hasOne, Row self) {
        QueryTable targetTable = new QueryTable(hasOne.targetSeed().getTableName());
        String inferTargetColumn = hasOne.inferTargetColumn();
        String selfField = StrUtil.toCamelCase(hasOne.inferSelfColumn());
        QueryCondition queryCondition = new QueryColumn(targetTable, inferTargetColumn).eq(self.get(selfField));
        Map<String, Object> extra = SeedKit.formatExtra(hasOne.getExtraSeed(), hasOne.getExtra());
        for(Map.Entry<String, Object> entry: extra.entrySet()) {
            queryCondition.and(new QueryColumn(targetTable, entry.getKey()).eq(entry.getValue()));
        }

        return queryCondition;
    }
}
